import 'package:FlutterReader/page/favoriteChildren/default_page.dart';
import 'package:FlutterReader/page/favoriteChildren/local_shelf.dart';
import 'package:FlutterReader/reading/EBookReader.dart';
import 'package:FlutterReader/reading/book_provider.dart';
import 'package:FlutterReader/wights/status_view.dart';
import 'package:flutter/material.dart';
import 'package:FlutterReader/router/context_extension.dart';
import 'package:provider/provider.dart';

import '../../generated/l10n.dart';

class ShowFavoritePage extends StatefulWidget {
  const ShowFavoritePage({super.key});

  @override
  State<ShowFavoritePage> createState() => _ShowFavoritePageState();
}

class _ShowFavoritePageState extends State<ShowFavoritePage> {
  late final StatusViewController _controller;
  String _appBarTitle = ""; // 初始标题
  Widget page = DefaultPage(); // 页面
  late BookProvider bookProvider;

  Widget _buildAppBarTitle() {
    if (_appBarTitle.trim().isEmpty) {
      return Text(S.current.favorite_subTitle);
    } else {
      return Text(_appBarTitle);
    }
  }

  @override
  void initState() {
    super.initState();
    _controller = StatusViewController();
    bookProvider = Provider.of<BookProvider>(context, listen: false);
  }

  // 本地书架的添加操作
  bool _openLocal = false;


  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).colorScheme;

    return Scaffold(
      appBar: AppBar(
        // 动态变化怎么实现
        title: _buildAppBarTitle(),
        actions: [
          IconButton(
            onPressed: () {

            },
            icon: Icon(Icons.search),
          ),
          SizedBox(width: 2),
          PopupMenuButton<String>(
            icon: Icon(Icons.more_vert_rounded), // 图标
            itemBuilder: (context) => [
              // 自定义菜单项（支持图标 + 文字）
              if (_openLocal) ...[
                PopupMenuItem<String>(
                  value: 'import',
                  child: ListTile(
                    leading: Icon(Icons.add, color: Colors.blue),
                    title: Text('导入本地书籍'),
                  ),
                ),
              ],
              PopupMenuItem<String>(
                value: 'read',
                child: ListTile(
                  leading: Icon(Icons.book_online, color: Colors.green),
                  title: Text('快速阅读'),
                ),
              ),
              PopupMenuItem<String>(
                value: 'new',
                child: ListTile(
                  leading: Icon(Icons.folder_copy_outlined, color: Colors.green),
                  title: Text('新建文件夹'),
                ),
              ),
            ],
            onSelected: (value) async {
              switch (value) {
                case 'import':
                  _controller.showLoading(); // 显示加载状态
                  try {
                    await bookProvider.convertFromTxt(); // 调用转换方法
                  } catch (e) {
                    print('Error: $e'); // 处理异常
                  } finally {
                    _controller.loaded(); // 隐藏加载状态
                  }
                  break;
                case 'read':
                  context.routeTo(EBookReader); // 跳转到阅读页面
                  break;
                case 'new':
                  print("新建文件夹");
                  break;
              }
            },
          ),
        ],
      ),
      drawer: Drawer(
        backgroundColor: theme.surface,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.zero, // 设置为直角
        ),
        elevation: 3,
        child: _buildChild(),
      ),
      body: page,
    );
  }

  // drawer视图
  Widget _buildChild() => ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      ListTile(
        title: Text("打开本地书架"),
        onTap: (){
          setState(() {
            _appBarTitle = '本地书架';
            _openLocal = true;
            page = LocalShelf();
          });
        },
      ),
      Divider(height: 20),
      ListTile(
        title: const Text('默认界面'),
        onTap: (){
          setState(() {
            _appBarTitle = '默认';
            _openLocal = false;
            page = DefaultPage();
          });
        },
      ),
      // 这里应该实现动态添加 ListTile
      // ListTile(
      //   title: Text('测试2'),
      //   onTap: (){
      //     setState(() {
      //       _appBarTitle = '测试2';
      //       _openLocal = false;
      //     });
      //   },
      // ),
      // ListTile(
      //   title: Text('测试3'),
      //   onTap: (){
      //     setState(() {
      //       _appBarTitle = '测试3';
      //       _openLocal = false;
      //     });
      //   },
      // ),
    ],
  );
}
